// gui.c

#include "gui.h"
#include "font.h"

// 广告元素位置
Area adArea = {40, 20, 280, 430};

// 商品组区域（左上角坐标 + 总区域大小）
Area goodGroupArea = {365, 20, 400, 430};

// 商品间距（水平、垂直）
Padding goodPadding = {50, 50};

// 商品元素位置数组（未初始化，需调用 initGoodArea）
Area goodArea[GOOD_NUM];

// 商品信息数组
GoodInfo goodsInfo[GOOD_NUM];

// 光标所在的商品下标
int cursorGoodIndex = 0;

//当前页面标识
PageRoute currentPage = MAIN_PAGE;

//当前支付类型
PayType currentPayType = WECHAT_PAY;

Area weChatPayImgArea = {484, 181, 180, 180};

// 初始化商品元素位置数组
void initGoodArea()
{
    int start_x = goodGroupArea.x;
    int start_y = goodGroupArea.y;
    // 三个一组
    for (int i = 0; i < GOOD_NUM; i++)
    {
        int count = i % 3;
        if (i < 3)
        {
            goodArea[i].x = start_x + count * (goodPadding.x + GOOD_WIDTH);
            goodArea[i].y = start_y;
        }
        else if (i < 6)
        {
            goodArea[i].x = start_x + count * (goodPadding.x + GOOD_WIDTH);
            goodArea[i].y = start_y + goodPadding.y + GOOD_HEIGHT;
        }
        else
        {
            goodArea[i].x = start_x + count * (goodPadding.x + GOOD_WIDTH);
            goodArea[i].y = start_y + 2 * goodPadding.y + GOOD_HEIGHT * 2;
        }

        goodArea[i].width = GOOD_WIDTH;
        goodArea[i].height = GOOD_HEIGHT;

        //特判
        if(i==6){
            goodArea[i].x+=15;
        }
    }
}

//初始化商品信息
void init_goods_info(void)
{ 
    const char *basePath = "./assert/images/";
    
    for (int i = 0; i < GOOD_NUM; i++) {
        snprintf(goodsInfo[i].picturePath, MAX_PATH_LEN, "%sgood_%d.bmp", basePath, i + 1);

        // printf("picturePath: %s\n", goodsInfo[i].picturePath);
        switch (i)  
        {
            case 0:
                goodsInfo[i].price = 6;
                strncpy(goodsInfo[i].name, "咖啡", MAX_NAME_LEN);
                break;

            case 1:
                goodsInfo[i].price = 3;
                strncpy(goodsInfo[i].name, "果汁", MAX_NAME_LEN);
                break;

            case 2:
                goodsInfo[i].price = 3.5;
                strncpy(goodsInfo[i].name, "饮料", MAX_NAME_LEN);
                break;

            case 3:
                goodsInfo[i].price = 4;
                strncpy(goodsInfo[i].name, "气泡水", MAX_NAME_LEN);
                break;

            case 4:
                goodsInfo[i].price = 5.5;
                strncpy(goodsInfo[i].name, "啤酒", MAX_NAME_LEN);
                break;

            case 5:
                goodsInfo[i].price = 3;
                strncpy(goodsInfo[i].name, "橙汁", MAX_NAME_LEN);
                break;

            case 6:
                goodsInfo[i].price = 6;
                strncpy(goodsInfo[i].name, "外星人", MAX_NAME_LEN);
                break;

            case 7:
                goodsInfo[i].price = 5.5;
                strncpy(goodsInfo[i].name, "牛奶", MAX_NAME_LEN);
                break;

            case 8:
                goodsInfo[i].price = 7;
                strncpy(goodsInfo[i].name, "鸡尾酒", MAX_NAME_LEN);
                break;

            default:
                goodsInfo[i].price = 0;
                strncpy(goodsInfo[i].name, "未知", MAX_NAME_LEN);
                break;
        }

        // 确保 name 结尾安全
        goodsInfo[i].name[MAX_NAME_LEN - 1] = '\0';
    }
}

//绘制按钮
void draw_round_button(int x, int y, int width, int height, const char *text)
{
    // 绘制背景
    fill_color(BUTTON_COLOR, x, y, width, height);

    // 绘制圆角边框（实线虚线长度相等即为实线）
    draw_dashed_border(x, y, width, height, BUTTON_COLOR, 2, BUTTON_RADIUS, 0);

    // 计算文本居中位置
    int text_x = x + (width - strlen(text) * 30) / 2;
    int text_y = y + (height - 30) / 2;

    // 显示文本
    font_display_text(text_x, text_y, text, 30, BUTTON_TEXT_COLOR);
}